    //
//  OrderViewController.m
//  AirTicket
//
//  Created by imac.bmw on 10-12-29.
//  Copyright 2010 aircococ. All rights reserved.
//

#import "OrderViewController.h"
#import "RootViewController.h"
#import "extver.h"

@implementation OrderViewController
@synthesize data;

static float Height;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];

	NSString *flightKind = [SaveDefaults objectForKey:@"singleOrDouble"];
	if ([flightKind isEqualToString:@"单程"]) {
		Height = 0;
		self.data = [infoMgr getDataForKey:@"单程航班"];

	}
	else {
		Height = 30;
		
		flightControl = [[UISegmentedControl alloc]initWithItems:nil];
		flightControl.frame = CGRectMake(50, 50, 200, 25);
		flightControl.segmentedControlStyle = UISegmentedControlStylePlain;
		flightControl.tintColor = [UIColor colorWithRed:0.75 green:1.0 blue:0.75 alpha:1.0];
		[flightControl insertSegmentWithTitle:@"去程" atIndex:0 animated:NO];
		[flightControl insertSegmentWithTitle:@"返程" atIndex:1 animated:NO];
		flightControl.selectedSegmentIndex = 0;
		[flightControl addTarget:self action:@selector(selectFlightControl) forControlEvents:UIControlEventValueChanged];
		[self.view addSubview:flightControl];
		[flightControl release];
		
	}

	
	//添加顶部信息
	UIImage *image = [UIImage imageNamed:@"orderhead.png"];
	UIImageView *head = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
	head.image = image;
	[self.view addSubview:head];
	[head release];
	
	UILabel *flightKindLabel = [[UILabel alloc]initWithFrame:CGRectMake(42, -7, 100, 35)];
	flightKindLabel.text = @"航程类型 --";
	flightKindLabel.font = [UIFont systemFontOfSize:13];
	flightKindLabel.textColor = [UIColor whiteColor];
	flightKindLabel.backgroundColor = [UIColor clearColor];
	[self.view addSubview:flightKindLabel];
	[flightKindLabel release];
	
	UILabel *singleOrDouble = [[UILabel alloc]initWithFrame:CGRectMake(110, -7, 30, 35)];
	singleOrDouble.text = [SaveDefaults objectForKey:@"singleOrDouble"];
	singleOrDouble.font = [UIFont systemFontOfSize:13];
	singleOrDouble.textColor = [UIColor whiteColor];
	singleOrDouble.backgroundColor = [UIColor clearColor];
	[self.view addSubview:singleOrDouble];
	[singleOrDouble release];
	
	headLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 13, 300, 35)];
	headLabel.backgroundColor = [UIColor clearColor];
	headLabel.font = [UIFont systemFontOfSize:13];
	headLabel.textColor = [UIColor whiteColor];
	[self.view addSubview:headLabel];
	[headLabel release];
	
	UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 50+Height, 50, 35)];
	label.text = @"航班:";
	[self.view addSubview:label];
	[label release];
	
	fltnoAirWayLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 50+Height, 245, 35)];
	fltnoAirWayLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:fltnoAirWayLabel];
	[fltnoAirWayLabel release];
	
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 80+Height, 50, 35)];
	label.text = @"起飞:";
	[self.view addSubview:label];
	[label release];
	
	deptimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 80+Height, 100, 35)];
	deptimeLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:deptimeLabel];
	[deptimeLabel release];
	
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(120, 80+Height, 50, 35)];
	label.text = @"到达:";
	[self.view addSubview:label];
	[label release];
	
	arrtimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(166, 80+Height, 80, 35)];
	arrtimeLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:arrtimeLabel];
	[arrtimeLabel release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 110+Height, 50, 35)];
	label.text = @"机型:";
	[self.view addSubview:label];
	[label release];
	
	planeLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 110+Height, 80, 35)];
	planeLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:planeLabel];
	[planeLabel release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(120, 110+Height, 50, 35)];
	label.text = @"配餐:";
	[self.view addSubview:label];
	[label release];
	
	mealLabel = [[UILabel alloc]initWithFrame:CGRectMake(166, 110+Height, 80, 35)];
	
	mealLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:mealLabel];
	[mealLabel release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 140+Height, 50, 35)];
	label.text = @"仓位:";
	[self.view addSubview:label];
	[label release];
	
	cangLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 140+Height, 80, 35)];
	cangLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:cangLabel];
	[cangLabel release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 190+Height, 100, 35)];
	label.text = @"剩余票数:";
	[self.view addSubview:label];
	[label release];
	
	ticketCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 190+Height, 80, 35)];
	ticketCountLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:ticketCountLabel];
	[ticketCountLabel release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(5, 220+Height, 100, 35)];
	label.text = @"机建/燃油:";
	[self.view addSubview:label];
	[label release];
	
	taxFuelLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 220+Height, 80, 35)];
	taxFuelLabel.textColor = [UIColor colorWithRed:0.01 green:0.45 blue:0.93 alpha:1.0f];
	[self.view addSubview:taxFuelLabel];
	[taxFuelLabel release];
	
	
	image = [UIImage imageNamed:@"orderfoot.png"];
	UIImageView *foot = [[UIImageView alloc]initWithFrame:CGRectMake(0, 288, image.size.width, image.size.height)];
	foot.image = image;
	[self.view addSubview:foot];
	[foot release];
	
	image = [UIImage imageNamed:@"totalprice.png"];
	foot = [[UIImageView alloc]initWithFrame:CGRectMake(4, 295, image.size.width, image.size.height)];
	foot.image = image;
	[self.view addSubview:foot];
	[foot release];
	
	label = [[UILabel alloc]initWithFrame:CGRectMake(10, 299, 140, 35)];
	if ([flightKind isEqualToString:@"单程"]) {
	label.text = [NSString stringWithFormat:@"单人总价:￥%d",
				  [data.price intValue]+[data.fuel intValue]+[data.tax intValue]];
	}
	else {
		CellNeedData *data1 = [[infoMgr getDataForKey:@"去程航班"] retain];
		CellNeedData *data2 = [[infoMgr getDataForKey:@"返程航班"] retain];
		label.text = [NSString stringWithFormat:@"单人总价:￥%d",
					  [data1.price intValue]+[data1.fuel intValue]+[data1.tax intValue] +
					  [data2.price intValue]+[data2.fuel intValue]+[data2.tax intValue]];
		[data1 release];
		[data2 release];
	}
	label.textColor = [UIColor darkGrayColor];
	label.font = [UIFont systemFontOfSize:18];
	label.backgroundColor = [UIColor clearColor];
	[self.view addSubview:label];
	[label release];
	
	image = [UIImage imageNamed:@"ordernow.png"];
	UIButton *order = [[UIButton alloc]initWithFrame:CGRectMake(109, 365, image.size.width, image.size.height)];
	[order setBackgroundImage:image forState:UIControlStateNormal];
	[order addTarget:self action:@selector(orderTicket) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:order];
	[order release];

	[self loadInfo];
}

-(void)selectFlightControl {

	if (flightControl.selectedSegmentIndex == 0) {
		self.data = [infoMgr getDataForKey:@"去程航班"];
	}
	else {
		self.data = [infoMgr getDataForKey:@"返程航班"];
	}
	
	[self loadInfo];

}

-(void)loadInfo {

	headLabel.text = [NSString stringWithFormat:@"%@   始发地:%@ -- 目的地:%@",data.date,[data.org convertCitycode],[data.dst convertCitycode]];
	fltnoAirWayLabel.text = [NSString stringWithFormat:@"%@   %@",data.flightNum,[data.airways covertAircode]];
	deptimeLabel.text = [NSString stringWithFormat:@"%@",data.deptime];
	arrtimeLabel.text = [NSString stringWithFormat:@"%@",data.arrtime];
	planeLabel.text = [NSString stringWithFormat:@"%@",data.planesty];
	NSString *meal = [NSString stringWithFormat:@"%@",data.meal];
	if ([meal isEqualToString:@"1"]) {
		meal = @"有";
	}
	else {
		meal = @"无";
	}
	
	mealLabel.text = meal;
	
	cangLabel.text = [NSString stringWithFormat:@"%@%@",data.classtype,data.cabin];
	ticketCountLabel.text = [NSString stringWithFormat:@"%@",([data.ticketsCount isEqualToString:@"A"]?@"大于9张":data.ticketsCount)];
	taxFuelLabel.text = [NSString stringWithFormat:@"%@/%@",data.tax,data.fuel];
	
}

-(void)orderTicket {

	AddInfoViewController *controller = [[AddInfoViewController alloc]init];
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[data release];
    [super dealloc];
}


@end
